#!/bin/bash
#set -x

# Shows you the largest objects in your repo's pack file.
# Written for osx.
#
# @see https://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
# @author Antony Stubbs

# set the internal field spereator to line break, so that we can iterate easily over the verify-pack output
IFS=$'\n';

# list all objects including their size, sort by size, take top 10 (or 1st provided argument)
objects=`git verify-pack -v .git/objects/pack/pack-*.idx | egrep -v 'chain|non delta: ' | sort -k3nr 2>/dev/null | head -n ${1:-10}`

echo "The pack column is the size of the object, compressed, inside the pack file."

#? set -x

i=0
echo "size,pack,SHA,location" | column -t -s ', '
for obj in $objects
do
	# extract the size in bytes
	sizeStr=$( echo $obj | cut -f 5 -d ' ' )
		if [ -z "$sizeStr" ]; then
			echo "ERROR! Size is empty! Incoming line: [$obj]"
			continue;
		fi
	size=$( numfmt --to=iec-i --suffix=B --format="%.3f" $sizeStr )
	# extract the compressed size in bytes
	compressedSizeStr=$(echo $obj | cut -f 6 -d ' ')
		if [ -z "$compressedSizeStr" ]; then continue; fi
	compressedSize=$( numfmt --to=iec-i --suffix=B --format="%.3f" ${compressedSizeStr:-0} )
	# extract the SHA
	sha=$( echo $obj | cut -f 1 -d ' ' )
	# find the objects location in the repository tree
	filename=$( git rev-list --all --objects | grep $sha )

	# output="${output}\n${size},${compressedSize},${other}"
		# Some reconds has no name (tree??). We skip them.
		if [ "$sha" = "$filename" ] || [ "$sha " = "$filename" ]; then continue; fi

	echo -e "${size},${compressedSize},${filename}" | column -t -s ', '

#	echo -e "${size},${compressedSize},=${filename}=,=$sha="
	# | column -t -s ', '

	if [ 0 -eq $(( ++i % 50 )) ]; then
		:
		# Debug progress each 50 items
#		echo $i 1>&2
	fi
done

echo "Total files: $i" 1>&2

#echo -e $output | column -t -s ', '
